package mybatisflex.codegen.factory;

import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.entity.Column;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刁海文
 * @Remark
 * @since 2023/7/19 15:39
 */
public class CustomerEntityGenerator implements IGenerator {
    private String templatePath = "/templates/enjoy/entityOrBase.tpl";

    private final String suffix;
    private final String pk;
    public CustomerEntityGenerator(String suffix, String pk) {
        this.suffix = suffix;
        this.pk = pk;
    }

    @Override
    public String getTemplatePath() {
        return templatePath;
    }

    @Override
    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }

    @Override
    public void generate(Table table, GlobalConfig globalConfig) {

        if (!globalConfig.isEntityGenerateEnable()) {
            return;
        }

        PackageConfig packageConfig = globalConfig.getPackageConfig();
        EntityConfig entityConfig = globalConfig.getEntityConfig();
        String entityName = table.getEntityJavaFileName() + suffix;
        String packageName = globalConfig.getBasePackage() + "." + pk;

        String entityPackagePath = packageName.replace(".", File.separator) + File.separator + entityName + ".java";
        File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath);


        if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) {
            return;
        }

        List<Column> columns = table.getColumns().stream()
                .filter(c -> !c.getProperty().equals("id"))
                .filter(c -> !c.getProperty().equals("created"))
                .filter(c -> !c.getProperty().equals("createdBy"))
                .filter(c -> !c.getProperty().equals("updatedBy"))
                .filter(c -> !c.getProperty().equals("updated"))
                .collect(Collectors.toList());

        table.setColumns(columns);
        entityConfig.setAlwaysGenColumnAnnotation(false);
        Map<String, Object> params = new HashMap<>(6);
        params.put("table", table);
        params.put("entityPackageName", packageName);
        params.put("entityConfig", entityConfig);
        params.put("entityClassName", entityName);
        params.put("packageConfig", packageConfig);
        params.put("javadocConfig", globalConfig.getJavadocConfig());
        params.put("isBase", false);

        if (suffix.equals("Query")){
            params.put("isQuery", true);
        }


        globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile);

        System.out.println("Entity ---> " + entityJavaFile);
        entityConfig.setAlwaysGenColumnAnnotation(true);

    }
}
